﻿using System;
using System.Collections;
using System.Data;
using System.Reflection;

namespace Dx.SqlAccess
{
    public sealed class ParameterCollection : MarshalByRefObject
    {
        private int intitialCapacity;
        private System.Collections.ArrayList items;

        public ParameterCollection()
        {
            this.intitialCapacity = 10;
        }

        public ParameterCollection(int initCapacity)
        {
            this.intitialCapacity = 10;
            this.intitialCapacity = initCapacity;
        }

        public QueryParameter Add(QueryParameter param)
        {
            this.ArrayList().Add(param);
            return param;
        }

        public QueryParameter Add(string ParameterName, object Value)
        {
            return this.Add(new QueryParameter(ParameterName, Value));
        }

        public QueryParameter Add(string ParameterName, object Value, DbType dbType)
        {
            return this.Add(new QueryParameter(ParameterName, Value, dbType));
        }

        private System.Collections.ArrayList ArrayList()
        {
            if (this.items == null)
            {
                this.items = new System.Collections.ArrayList(this.intitialCapacity);
            }
            return this.items;
        }

        public void Clear()
        {
            this.ArrayList().Clear();
        }

        public int IndexOf(string ParameterName)
        {
            if (this.items != null)
            {
                for (int i = 0; i < this.items.Count; i++)
                {
                    if (((QueryParameter) this.items[i]).ParameterName.Equals(ParameterName))
                    {
                        return i;
                    }
                }
            }
            return -1;
        }

        private void RangeCheck(int index)
        {
            if ((index < 0) || (this.Count <= index))
            {
                throw new IndexOutOfRangeException("Number " + index.ToString() + " is out of Range");
            }
        }

        private int RangeCheck(string ParameterName)
        {
            int index = this.IndexOf(ParameterName);
            if (index < 0)
            {
                throw new IndexOutOfRangeException("ParameterName " + ParameterName + " dose not exist");
            }
            return index;
        }

        private void Replace(int index, QueryParameter newValue)
        {
            this.Validate(index, newValue);
            this.items[index] = newValue;
        }

        private void Validate(int index, QueryParameter Value)
        {
        }

        private void ValidateType(object Value)
        {
        }

        public int Count
        {
            get
            {
                if (this.items == null)
                {
                    return 0;
                }
                return this.items.Count;
            }
        }

        public QueryParameter this[string ParameterName]
        {
            get
            {
                int num = this.RangeCheck(ParameterName);
                return (QueryParameter) this.items[num];
            }
            set
            {
                int index = this.RangeCheck(ParameterName);
                this.Replace(index, value);
            }
        }

        public QueryParameter this[int index]
        {
            get
            {
                this.RangeCheck(index);
                return (QueryParameter) this.items[index];
            }
            set
            {
                this.RangeCheck(index);
                this.Replace(index, value);
            }
        }
    }
}

